import { WithNormalRender } from "@ey-render/hoc-form";
import { useMemo, FC, useEffect, useRef } from 'react';
import { RouteComponentProps } from 'react-router';
import { getSubmitFormValues, getFormValues } from '@ey-utils/utils/formily-utils/getSubmitFormValues';
import { history } from 'umi';
import {
  useBillParams,
  useBillBreadcrumbEvent,
  orgChange,
} from '@/pages/invoice/hooks';

import components from "@/components/schema-form-render/schema-components-registed/components-biz";

import { ArcvBorrowConfig, ArcvBorrowI18N, arcvBorrowSubmit, arcvBorrowSave, findArcvJson } from './service';
import { Form } from '@formily/core/esm/models';
import { onFormInit, onFieldValueChange } from '@formily/core';

interface ArcvBorrowCreateInvoiceProps extends RouteComponentProps  {

}

const ArcvBorrowCreateInvoice: FC<ArcvBorrowCreateInvoiceProps> = (props) => {
  // 处理 各个入口单据 参数不一致问题
  const { type, pageType } = useBillParams(props);
  const formRef = useRef<Form<any>>();

  // 通知 BasicLayout 生成对应的面包屑
  useBillBreadcrumbEvent(ArcvBorrowI18N[type] || ArcvBorrowI18N, type);

  const BillFormLayout = useMemo(() => {
    return WithNormalRender({
      filedConfig: {},
      components: {
        ...components
      },
    });
  }, []);

  // 提交
  const submit = async ({ form }: any) => {
    const formValues = await getSubmitFormValues(form);

    if (formValues) {
      const params = {
        ...formValues,
        pageCode: ArcvBorrowConfig.CREATE,
        type,
      };

      const res: any = await arcvBorrowSubmit(params);

      const { success } = res || {};

      if (success) {
        history.push('/archives/my-borrow');
      }

      return res;
    }

    return {
      success: false,
    };
  };
  // 保存
  const save = async ({ form }: any) => {
    const formValues: any = await getFormValues(form);
    if (formValues) {
      const params = {
        ...formValues,
        pageCode: ArcvBorrowConfig.CREATE,
        type,
      };
      const res: any = await arcvBorrowSave(params);

      const { success } = res || {};

      if (success) {
        history.push('/archives/my-borrow');
      }

      return res;
    }

    return {
      success: false,
    };
  };

  return (
    <BillFormLayout
      anchor
      type="NORMAL"
      pageCode={ArcvBorrowConfig.CREATE}
      events={{
        submit,
        save
      }}
      formConfigs={{
        values: {
          pageType,
        },
        effects: () => {
          onFormInit((form) => {
            formRef.current = form;
          })
          orgChange({
            user: 'brwUser',
            userName: 'brwUserName',
            company: 'applyUserCo',
            companyName: 'applyUserCoName',
            department: 'applyUserDpt',
            departmentName: 'applyUserDptName',
          })
          onFieldValueChange('operationAuth', (field, form) => {
            if (!field.modified) return;

            const names = field.inputValues?.[1]?.map(item => {
              return item.name
            }).join(',')

            const times = setTimeout(() => {
              clearTimeout(times);
              form.setValuesIn('operationAuthName', names);
            });
          })
        },
      }}
      filedConfig={{}}
      eventMap={{}}
    />
  );
};

export default ArcvBorrowCreateInvoice;
